package com.axc.gallery2.view;


import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.axc.gallery2.R;
import com.axc.gallery2.net.activity.NetAlbumsActivity;
import com.axc.gallery2.view.ImageAdapter.ViewHolder;
import com.renren.api.connect.android.Renren;
import com.renren.api.connect.android.photos.PhotoUploadRequestParam;




public class UploadPhotosAdapter extends BaseAdapter {
	
	PhotoUploadRequestParam photoParam;
	
	private Context mContext;
	protected int editPositon =-1;
	Renren renren;

	public Context getContext() {
		return this.mContext; 
	}

	public void setContext(Context context) {
        this.mContext =context;
    }
	List<String> list;
	List<Integer> statelList;

	public List<String> getList(){
		return list;
	}
	
	public List<Integer> getStatelList(){
		return statelList;
	}

	public UploadPhotosAdapter(Context context,List<String> lists) {

		renren = new Renren(NetAlbumsActivity.getApiKey(), NetAlbumsActivity.getSecretKey(), NetAlbumsActivity.getAppId(), context);
		mContext = context;		
		list=lists;
		statelList =new ArrayList<Integer>();
		for (int i=0;i< lists.size();i++) {
			statelList.add(0);
		}
	}
	
	/**
	 * 添加数据
	 * @param lists
	 */
	public void addItems(List<String> lists) {
		for (int i=0;i< lists.size();i++) {
			statelList.add(0);
			this.list.add(lists.get(i));
		}
		
	}

	/**
	 * 设置状态
	 * @param position
	 * @param state 0:未开始;1:完成;2:正在进行
	 */
	public void SetState(int position,int state) {
		statelList.set(position, state);
		notifyDataSetChanged();
	}

	public int getCount() {
		return list.size();
	}

	public Object getItem(int position) {
		return position;
	}


	public long getItemId(int position) {
		return position;
	}

	public void removeAt(int position) {
		list.remove(position);
		notifyDataSetChanged();
	}
	public void editItem(int position,Object item) {
		list.set(position, (String) item);
		notifyDataSetChanged();
	}
	public void add(Object item) {
		list.add(getCount(),(String) item);
		notifyDataSetChanged();
	}
	List<SoftReference<View>> convertviewList =new ArrayList<SoftReference<View>>();

	public List<SoftReference<View>> getConvertviewList() {
		return convertviewList;
	}

	public void setConvertviewList(List<SoftReference<View>> convertviewList) {
		this.convertviewList = convertviewList;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		if(getCount()==0){
			TextView t =new TextView(mContext);
			t.setText("没有任务！");
			t.setLinkTextColor(Color.RED);
			t.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 200));
			return t;
		}
		final ViewHolder viewHolder;

			viewHolder =new ViewHolder();
			convertView =LayoutInflater.from(this.getContext())
			.inflate(R.layout.upload, null);
			convertView.setTag(viewHolder);
			viewHolder.drTextView = (TextView) convertView.findViewById(R.id.upload_textView);
			viewHolder.progressBar = (ProgressBar) convertView.findViewById(R.id.upload_progressBar);
			viewHolder.button = (Button) convertView.findViewById(R.id.upload_button);
			viewHolder.button.setFocusable(true);
			viewHolder.button.setClickable(true);
			viewHolder.button1 = (Button) convertView.findViewById(R.id.upload_remove);
			viewHolder.button1.setFocusable(true);
			viewHolder.button1.setVisibility(View.GONE);
			viewHolder.button1.setClickable(true);
			//设置点击事件
			viewHolder.button.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					onUploadListener.onUplaod(v, position);
				}
			});
			//移除
			viewHolder.button1.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					removeAt(position);
					statelList.remove(position);
				}
			});
			switch (statelList.get(position)) {
			case 0://未开始
				viewHolder.button.setText("开始");
				viewHolder.progressBar.setVisibility(View.INVISIBLE);
				viewHolder.button.setEnabled(true);
				viewHolder.drTextView.setText("等待...");
				viewHolder.button1.setVisibility(View.VISIBLE);
				break;
			case 1://完成
				viewHolder.button.setText("移除");
				viewHolder.button.setEnabled(true);
				viewHolder.progressBar.setVisibility(View.INVISIBLE);
				viewHolder.drTextView.setText("完成");
				break;
			case 2://正在进行
				viewHolder.button.setText("取消");
				viewHolder.button.setEnabled(true);
				viewHolder.progressBar.setVisibility(View.VISIBLE);
				viewHolder.drTextView.setText("正在上传...");
				break;
			}

		
			viewHolder.imageView =(ImageView) convertView.findViewById(R.id.upload_imageView);
			new AsyncLoadImage().execute(new Object[] { viewHolder.imageView,
                    list.get(position) });

			return convertView;

	}

	OnUploadListener onUploadListener ;

	
	public OnUploadListener getOnUploadListener() {
		return onUploadListener;
	}

	public void setOnUploadListener(OnUploadListener onUploadListener) {
		this.onUploadListener = onUploadListener;
	}


	/**
	 * 上传接口
	 * @version 1.0
	 * @author 伟贤
	 * @datatime 2013-1-30
	 */
	public interface OnUploadListener{
		void onUplaod(View view,int position);
	}

}
